package lib.operation;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @author FBI
 * 2022-02-06 20:53
 */


public class ReturnBook implements Do{
    @Override
    public void work() {
        System.out.println("=====借阅书籍=====");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入书名");
        String name = sc.nextLine();  //name为所要借阅书名

        //还
        try {
            ReturnBook.reBook(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //还书
    public static void reBook (String name) throws Exception {
        Connection conn = null;
        Statement stmt = null;
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/lib", "root", "2506642408");
        stmt = conn.createStatement();
        String sql = "update book set isBorrowed = 1 where name = "+"'"+ name + "'";
        int count = stmt.executeUpdate(sql);
        System.out.println(count == 1 ? "归还成功":"归还失败");
    }
}
